package org.example.shop_front_api.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class ShopInterceptor implements HandlerInterceptor {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        /*
        String authorization = request.getHeader("authorization");
        String token = authorization.substring(7);
        //redis token
        String redisToken = stringRedisTemplate.opsForValue().get(token);
        if (redisToken == null || !redisToken.equals(token)) {
            JsonResult msg = JsonResult.failed("token错误,没权限访问");
            response.setContentType("text/json;charset=utf-8");
            Writer writer = response.getWriter();
            Gson gson = new Gson();
            String jsonMsg = gson.toJson(msg);
            writer.write(jsonMsg);
            writer.flush();

            return false;
        }
        */
        //true 通过拦截，false 拦截成功
        System.out.println("preHandle");
        return true;
    }
}
